//----------------------------------------------------------------------------
// James Mathews
// 3/10/2010
// New prospeckz template 
// Based on code from Steven Wong and Matthew Barnes
//----------------------------------------------------------------------------

//PSoc port defintions
#include "psocapi.h"
#include "psocgpioint.h"

//Prospeckz api
#include "prospeckz.h"
#include "prospeckz_radio.h"
#include "m8c.h"

#include "PSoCAPI.h"    // PSoC API definitions for all User Modules  

//LCD
#include "lcd.h"
#include "bitmap.h"

//Radio
#include "StopAndWait.h"
#include "xlowpan.h"
#include "MAC.h"

//Memory management
#include "dynmem.h"

unsigned char _memory1[256];
unsigned char _memory2[256];
//unsigned char _memory3[256];
//unsigned char _memory3[128];
//unsigned char _memory4[256];

struct xlowpan_addr64 MyAddr;
struct xlowpan_addr64 TargetAddr;


unsigned char B0_Pressed(void){
	B0_Data_ADDR &= ~B0_MASK;	//force low
	if (B0_Data_ADDR & B0_MASK)
		return TRUE;
	else
		return FALSE;		
}

unsigned char B1_Pressed(void){
	B1_Data_ADDR &= ~B1_MASK;	//force low
	if (B1_Data_ADDR & B1_MASK)
		return TRUE;
	else
		return FALSE;		
}

unsigned char B2_Pressed(void){
	B2_Data_ADDR &= ~B2_MASK;	//force low
	if (B2_Data_ADDR & B2_MASK)
		return TRUE;
	else
		return FALSE;		
}

void GotPacket(struct xlowpan_addr64 *from, unsigned char* data, unsigned int length)
{

	LED_Off;
	LED_GREEN_On;
	lcd_print_rle_bitmap(RLEImage2);
}

unsigned int SendRadio(struct xlowpan_addr64 *to, void* data, unsigned int length)
{
	return xlowpan_send(to, data,length);
}


void main(void)  
{  
	int pressed = 0;
	int x = 0;
	int y = 0;

	lcd_do_setup();
	
	lcd_set_pos(0,0);

	MyAddr.addr[0] = '!';
	MyAddr.addr[1] = 'w';
	MyAddr.addr[2] = 'e';
	MyAddr.addr[3] = 's';
	MyAddr.addr[4] = 'o';
	MyAddr.addr[5] = 'm';
	MyAddr.addr[6] = 'e';
	MyAddr.addr[7] = 1;
	
	TargetAddr.addr[0] = '!';
	TargetAddr.addr[1] = 'w';
	TargetAddr.addr[2] = 'e';
	TargetAddr.addr[3] = 's';
	TargetAddr.addr[4] = 'o';
	TargetAddr.addr[5] = 'm';
	TargetAddr.addr[6] = 'e';
	TargetAddr.addr[7] = 2;

	dynmem_init(_memory1, 256);
	dynmem_append(_memory2, 256);
	//dynmem_append(_memory3, 128);
	//dynmem_append(_memory4, 256);

	// Turn off LED for power...
/*		
	M8C_EnableGInt;
	M8C_EnableIntMask(INT_MSK0,INT_MSK0_GPIO);	//Enable interupts on the GPIO port
	
	Reliable_Init(&MyAddr,10);

	Reliable_SetReceiveHandler(&GotPacket);
	Reliable_SetSendHandler(&SendRadio);
	
	M8C_EnableGInt;
	M8C_EnableIntMask(INT_MSK0,INT_MSK0_GPIO);	//Enable interupts on the GPIO port
*/	
	LED_Off;
	
	lcd_print_rle_bitmap_pos(FACE, 16,16);
	
	
	while(1)
	{
		if(SWITCH_Pressed() && !pressed)
		{
			y++;
			if(y > 7) y = 7;
			lcd_clear();
			lcd_print_rle_bitmap_pos(FACE,x,y);
		}
		if(B0_Pressed() && !pressed)
		{
			y--;
			if(y < 0) y = 0;
			lcd_clear();
			lcd_print_rle_bitmap_pos(FACE,x,y);
		}
		if(B2_Pressed() && !pressed)
		{
			x++;
			if(x > 127) x = 127;
			lcd_clear();
			lcd_print_rle_bitmap_pos(FACE,x,y);
		}
		if(B1_Pressed() && !pressed)
		{
			x--;
			if(x < 0) x = 0;
			lcd_clear();
			lcd_print_rle_bitmap_pos(FACE,x,y);
		}
		
		if(pressed && !SWITCH_Pressed() &&  !B0_Pressed() && !B1_Pressed() && !B2_Pressed()) pressed = 0;
	}
	
	return;
	while(1)
	{
		while(MAC_CheckRadio());
		if((B0_Pressed() || SWITCH_Pressed()) && !pressed)
		{
			unsigned char packet[2];
			packet[0] = 1;
			packet[1] = 2;
			
			LED_Off;
			LED_BLUE_On;
			if(!Reliable_TransmitPacket(&TargetAddr,packet,2, 50000, 5))
			{
				LED_Off;
				LED_GREEN_On;
				lcd_print_rle_bitmap(RLEImage2);
			}
			else
			{
				LED_Off;
				LED_RED_On;
			}
		}
		if(B2_Pressed() && !pressed)
		{
			
		}
		if(B1_Pressed() && !pressed)
		{
			lcd_clear();
			pressed = 1;
		}
		
		if(pressed && !SWITCH_Pressed() &&  !B0_Pressed() && !B1_Pressed() && !B2_Pressed()) pressed = 0;
	}
	
}

void string_move(char *o, char *i){
	int q;
	for (q = 0; q < 14; q++){
	
		*o = *i;
		o++; 
		i++;
	}
}

#pragma interrupt_handler PSoC_GPIO_ISR
void PSoC_GPIO_ISR(void){
	M8C_DisableGInt;
	lcd_clear();
	lcd_set_pos(0,0);
	lcd_print_cstring("interrupt!");
	while(MAC_CheckRadio());
	M8C_EnableGInt;
}
